package repository

import (
    "github.com/uniplaces/carbon"
    "hitme/go-api/models"
)

type orderRep struct {
    BaseRepo
}

var OrderRep = &orderRep{BaseRepo: BaseRepo{
    IModel: models.OrderModel,
}}

// Create 创建订单
func (r *orderRep) Create(data models.Order) (models.Order, error) {
    err := r.Model().Create(&data).Error
    return data, err
}

// GetList 获取订单列表
func (r *orderRep) GetList(uid uint64) []models.Order {
    var data []models.Order
    r.Model().Where("uid = ?", uid).Where("created_at > ?", carbon.Now().SubDays(7).DateTimeString()).Select("id, order_sn, amount, created_at, photo_url, photo_wm_url, spec_id, status, title, cloth").Order("id desc").Find(&data)
    return data
}

// GetOrderInfo 获取订单详情
func (r *orderRep) GetOrderInfo(orderSn string) models.Order {
    var data models.Order
    r.Model().Where("order_sn = ?", orderSn).Select("id, uid, price, quantity, order_sn, amount, pay_at, pic_id, fail_reason, color, is_fair, created_at, photo_url, photo_wm_url, spec_id, status, title, cloth").First(&data)
    return data
}

// GetOrderInfoDetail 获取订单详情
func (r *orderRep) GetOrderInfoDetail(orderSn string) models.Order {
    var data models.Order
    r.Model().Where("order_sn = ?", orderSn).First(&data)
    return data
}

// Update 更新订单
func (r *orderRep) Update(orderSn string, data models.Order) error {
    return r.Model().Where("order_sn = ?", orderSn).Updates(&data).Error
}